//
//  HMViewController.m
//  02-按钮操作
//
//  Created by 刘凡 on 14/8/11.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "HMViewController.h"

typedef enum
{
    kMovingDirTop = 10,
    kMovingDirBottom,
    kMovingDirLeft,
    kMovingDirRight,
}kMovingDir;

#define kMovingDelata   100

@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIButton *iconButton;

@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(110, 80, 100, 100)];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];
    [btn setTitle:@"点我啊" forState:UIControlStateNormal];
    [btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;

    [self.view addSubview:btn];
    
    self.iconButton = btn;
}

/** 上下左右移动 */
- (IBAction)move:(UIButton *)button
{
//    CGRect frame = self.iconButton.frame;
//    CGPoint center = self.iconButton.center;
    CGFloat tx = 0, ty = 0;
    switch (button.tag) {
        case kMovingDirTop:
            ty -= kMovingDelata;
            break;
        case kMovingDirBottom:
            ty += kMovingDelata;
            break;
        case kMovingDirLeft:
            tx -= kMovingDelata;
            break;
        case kMovingDirRight:
            tx += kMovingDelata;
            break;
    }
//    self.iconButton.center = center;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0f];
    self.iconButton.transform = CGAffineTransformTranslate(self.iconButton.transform, tx, ty);
    [UIView commitAnimations];
}

/** 放大缩小 */
- (IBAction)zoom:(UIButton *)button
{
//    CGRect frame = self.iconButton.bounds;
//    
//    CGFloat zoom = 20.0;
//    if (button.tag) {
//        zoom *= -1;
//    }
//    frame.size.width += zoom;
//    frame.size.height += zoom;
    CGFloat scale = (button.tag == 1) ? 0.8 : 1.2;
    
    [UIView beginAnimations:nil context:nil];
    
    self.iconButton.transform = CGAffineTransformScale(self.iconButton.transform, scale, scale);
    
    [UIView commitAnimations];
}

/** 旋转 */
- (IBAction)rotate:(UIButton *)button
{
    CGFloat angle = (button.tag == 1) ? M_PI_4 : -M_PI_4;
    
    [UIView beginAnimations:nil context:nil];
    
    self.iconButton.transform = CGAffineTransformRotate(self.iconButton.transform, angle);
    
    [UIView commitAnimations];
}

@end
